<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\model\BaseModel;


class ZoneConfigModel extends BaseModel
{
    // 表名
    protected $name = 'tongcheng_zone_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'open_location_text',
        'open_region_limit_text',
        'open_rider_enter_text',
        'open_rider_enter_audit_text',
        'open_zone_delivery_fee_rate_text',
    ];


    public function getOpenLocationList()
    {
        return ['1' => __('Open_location 1'), '0' => __('Open_location 0')];
    }

    public function getOpenRegionLimitList()
    {
        return ['1' => __('Open_region_limit 1'), '0' => __('Open_region_limit 0')];
    }

    public function getOpenRiderEnterList()
    {
        return ['1' => __('Open_rider_enter 1'), '0' => __('Open_rider_enter 0')];
    }

    public function getOpenRiderEnterAuditList()
    {
        return ['1' => __('Open_rider_enter_audit 1'), '0' => __('Open_rider_enter_audit 0')];
    }

    public function getOpenZoneDeliveryFeeRateList()
    {
        return ['1' => __('open_zone_delivery_fee_rate 1'), '0' => __('open_zone_delivery_fee_rate 0')];
    }


    public function getOpenLocationTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_location']) ? $data['open_location'] : '');
        $list  = $this->getOpenLocationList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenRegionLimitTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_region_limit']) ? $data['open_region_limit'] : '');
        $list  = $this->getOpenRegionLimitList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenRiderEnterTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_rider_enter']) ? $data['open_rider_enter'] : '');
        $list  = $this->getOpenRiderEnterList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenRiderEnterAuditTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_rider_enter_audit']) ? $data['open_rider_enter_audit'] : '');
        $list  = $this->getOpenRiderEnterAuditList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getOpenZoneDeliveryFeeRateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_zone_delivery_fee_rate']) ? $data['open_zone_delivery_fee_rate'] : '');
        $list  = $this->getOpenZoneDeliveryFeeRateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function zone()
    {
        return $this->belongsTo(ZoneModel::class, 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 获取区域配送费抽佣比例配置
     * @param $zone_id
     * @param $config
     * @return double
     */
    public static function getZoneDeliveryFeeRate($zone_id, $config = null)
    {
        $config = $config ?: self::get(['zone_id' => $zone_id]);
        if (!$config) {
            return 0;
        }
        if (!($config['open_zone_delivery_fee_rate'] ?? 0)) {
            return 0;
        }

        return $config['zone_delivery_fee_rate'];
    }

    /**
     * 获取区域骑手承担微信手续费比例配置
     * @param $zone_id
     * @return double
     */
    public static function getRiderWxFeeRate($zone_id)
    {
        $config = self::get(['zone_id' => $zone_id]);
        if (!$config) {
            return 0;
        }
        if (!$config['open_rider_wx_fee']) {
            return 0;
        }

        return $config['rider_wx_fee_rate'];
    }
}
